import request from "@/utils/request";
import {ResourceModel} from "@/model/ResourceModel";
import {PagingModel} from "@/model/PagingModel";


export default class ResourceApi {

    /**
     * 新建文件夹
     */
    public static createFolder(parentId:string, name: string, description: string, deptId: string): Promise<ResourceModel> {
        return new Promise((resolve, reject) => {
            request({
                url: '/broadcast/resource/folder/save',
                method: 'post',
                data: {
                    parentId: parentId || undefined,
                    resName: name,
                    description: description || undefined,
                    deptId
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data)
                }else {
                    reject(res)
                }
            }, (err) => {
                reject(err)
            })
        })
    }

    /**
     * 查询文件列表
     * @param folderId
     * @param searchKey
     * @param pageIndex
     * @param pageSize
     */
    public static listFile(folderId: string,deptId: string,  searchKey: string = null, pageIndex: number  = 1, pageSize: number = 30): Promise<PagingModel<ResourceModel>> {
        return new Promise((resolve, reject) => {
            request({
                url: '/broadcast/resource/list',
                method: 'get',
                params: {
                    parentId: folderId || undefined,
                    deptId,
                    searchKey,
                    pageNum: pageIndex,
                    pageSize
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    res.rows = res.rows.map(item => new ResourceModel().deserialize(item))
                    res.pageIndex = pageIndex
                    res.total = +res.total
                    res.pageSize = pageSize
                    resolve(res)
                } else {
                    reject(res)
                }
            }, (err) => {
                reject(err)
            })
        })
    }

    /**
     * 查询文件列表
     * @param fileId
     */
    public static selectFileById(fileId: string): Promise<ResourceModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/resource/${fileId}`,
                method: 'get',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new ResourceModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, (err) => {
                reject(err)
            })
        })
    }

    /**
     * 查询文件列表
     * @param fileId
     */
    public static listConvertData(fileId: string): Promise<string[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/resource/${fileId}/convert/list`,
                method: 'get',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }, (err) => {
                reject(err)
            })
        })
    }

    /**
     * 批量添加文件
     */
    public static batchSave(folderId: string = null, listFile: ResourceModel[]): Promise<ResourceModel[]> {
        let url = `/broadcast/resource/batch/save`
        if (folderId) {
            url += `?folderId=${folderId}`
        }
        return new Promise((resolve, reject) => {
            request({
                url: url,
                method: 'post',
                data: listFile
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(res.data ? res.data.map(item => new ResourceModel().deserialize(item)) : [])
                }else {
                    reject(res)
                }
            }, (err) => {
                reject(err)
            })
        })
    }

    /**
     * 转换资源
     * @param fileId
     */
    public static translateResource(fileId: string): Promise<ResourceModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/resource/translate/${fileId}`,
                method: 'post',
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new ResourceModel().deserialize(res.data))
                }
            })
        })
    }

    /**
     * 一般ts都是这种，而且这东西不是你打开的么
     * 删除模板
     * @param fileId
     */
    public static deleteTemplate(fileId: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/resource/${fileId}`,
                method: 'delete',
            }).then((res: any) => {
                resolve(res)
            })
        })
    }

    /**
     * 根据文件路径删除文件
     * @param listPath
     */
    public static deleteTempByFilePath(listPath: string[]): Promise<number> {
        return new Promise<number>((resolve, reject) => {
            request({
                url: '/broadcast/resource/delete',
                method: 'delete',
                data: listPath
            }).then((res: any) => {
                resolve(res)
            })
        })
    }


}




